using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using Common;

public partial class tm_TopNewList : System.Web.UI.UserControl
{
    private static int _curPortalId = -1;
    public int curPortalId
    {
        get { return _curPortalId; }
        set { _curPortalId = value; }
    }

    public enum newsType : int
    {   News,
        Story,
        Advice,
        SmallAd,
        Product,
        BuyRequest
    }
    public void SetType(newsType type)
    {
        switch (type)
        {
            case newsType.Advice:
                lblNewsType.Text = "AD"; break;
            case newsType.Story:
                lblNewsType.Text = "ST"; break;
            case newsType.News:
                lblNewsType.Text = "NE"; break; 
            case newsType.Product :
                lblNewsType.Text = "PR"; break; 
            case newsType.BuyRequest :
                lblNewsType.Text = "BR"; break; 
            case newsType.SmallAd:
                lblNewsType.Text = "SA"; break; 
        }
    }

    public int ShowList(newsType type)
    {
        SetType(type);
        return ShowList();
    }

    public int ShowList()
    {
        try
        {
            string newsType = "";
            string SQLCmd = "";
            switch (lblNewsType.Text.Trim())
            {
                case "NE": newsType = Common.consts.constNewsTypeNews;
                    SQLCmd = "SELECT TOP " + Common.systemSettings.sysNoOfTopNews +
                            " newsId AS infoId,subject AS description FROM tm_news" +
                            " WHERE newsType='" + newsType + "' AND Status = '" + Common.consts.constStatusENABLE + "' AND closeDate > " + Common.sysLibs.ConvertToSQLDateString(DateTime.Now) + "'" + 
                            " ORDER BY createDate DESC";
                    break;

                case "AD": newsType = Common.consts.constNewsTypeAdvice;
                    SQLCmd ="SELECT TOP " + Common.systemSettings.sysNoOfTopNews +
                            " newsId AS infoId,subject AS description FROM tm_news" +
                            " WHERE newsType='" + newsType + "' AND Status = '" + Common.consts.constStatusENABLE + "' AND closeDate > " + Common.sysLibs.ConvertToSQLDateString(DateTime.Now) + "'" + 
                            " ORDER BY createDate DESC";
                    break;
                case "ST": newsType = Common.consts.constNewsTypeStory;
                    SQLCmd ="SELECT TOP " + Common.systemSettings.sysNoOfTopNews +
                            " newsId AS infoId,subject AS description FROM tm_news" +
                            " WHERE newsType='" + newsType + "' AND Status = '" + Common.consts.constStatusENABLE + "' AND closeDate > " + Common.sysLibs.ConvertToSQLDateString(DateTime.Now) + "'" + 
                            " ORDER BY createDate DESC";
                    break;
                case "PR":
                    SQLCmd = "SELECT TOP " + Common.systemSettings.sysDefaultTopNewProductNum +
                             " productId AS infoId,name1 AS description FROM tm_product "+
                             " WHERE status = '" + Common.consts.constStatusENABLE + "' AND expireDate > '" + Common.sysLibs.ConvertToSQLDateString(DateTime.Now) + "'" + 
                             " ORDER BY lastModifyDate DESC";
                    break; ;
                case "BR":
                    //hoang 18 May 2009
                    SQLCmd = "SELECT TOP " + Common.systemSettings.sysDefaultTopNewBuyRequestNum + " a.requestId AS infoId ,a.Name1 AS description,c.description1 AS status," +
                                   " a.RegisterDate,ISNULL(a.lastModifyDate,'') AS lastModifyDate,ISNULL(a.expireDate,'') AS expireDate," +
                                   " a.registerBy AS buyerId," +
                                   " CASE WHEN b.companyName1 IS NULL THEN b.fullName ELSE b.companyName1 END AS buyerName " +
                                   " FROM dbo.tm_buy_request a INNER JOIN  tm_users b ON a.RegisterBy = b.userId" +
                                   " INNER JOIN tm_buyStatus c ON a.status = c.statusCode " +
                                   " WHERE (a.expireDate IS NULL OR a.expireDate > '" + sysLibs.ConvertToSQLDateString(DateTime.Now) + "')" + 
                                   " ORDER BY registerDate DESC";
                    break; ;
                case "SA":
                    SQLCmd = "SELECT TOP " + Common.systemSettings.sysDefaultTopNewSmallAdsNum +
                             " advertId AS infoId, subject AS description FROM tm_small_advert " +
                             " WHERE status = '" + Common.consts.constStatusENABLE + "' AND closeDate > " + Common.sysLibs.ConvertToSQLDateString(DateTime.Now) + "'" + 
                             " ORDER BY openDate DESC";
                    break; ;
            }
            dsTopNewList.ConnectionString = Common.sysLibs.GetConnectionString;
            dsTopNewList.SelectCommand = SQLCmd;
            dsTopNewList.Select(DataSourceSelectArguments.Empty);
            gvTopNewList.DataBind();
            return gvTopNewList.Rows.Count;
        }
        catch { }
        return 0;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //ShowList();
    }

    protected string getURL(string infoId)
    {
        int myTabId=0, myModuleId=0;
        switch (lblNewsType.Text.Trim())
        {
            case "NE": 
            case "AD": 
            case "ST": 
                  myTabId = Common.appLibs.GetTabIdByName(curPortalId, "news");
                  myModuleId = Common.appLibs.GetModuleIdByName(curPortalId, "news");
                  return DotNetNuke.Common.Globals.NavigateURL(myTabId, "", "newsId=" + infoId.ToString());
            case "PR":
                myTabId = Common.appLibs.GetTabIdByName(curPortalId, "product");
                myModuleId = Common.appLibs.GetModuleIdByName(curPortalId, "product");
                return DotNetNuke.Common.Globals.NavigateURL(myTabId, "viewdetail", "mid=" + myModuleId, "productId=" + infoId.ToString());

            case "BR":
                myTabId = Common.appLibs.GetTabIdByName(curPortalId, "buyRequest");
                myModuleId = Common.appLibs.GetModuleIdByName(curPortalId, "buyRequest");
                return DotNetNuke.Common.Globals.NavigateURL(myTabId, "viewdetail", "mid=" + myModuleId, "requestId=" + infoId.ToString());
            case "SA":
                myTabId = Common.appLibs.GetTabIdByName(curPortalId, "smalladvert");
                myModuleId = Common.appLibs.GetModuleIdByName(curPortalId, "smalladvert");
                return DotNetNuke.Common.Globals.NavigateURL(myTabId, "", "advertId=" + infoId.ToString());
        }
        return "";
    }
}
